<?php
namespace BoxSns\Controller\Home;
//use Think\Controller;
class IndexController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$this->nav_cur = "Home";
	}

	public function index() {
		$this->group_name = "声声慢社区";
		$this->group_ico = "/group/15096.jpg";
		$this->group_count = BoxModel("sns_topic")->where("store_id='$this->store_id' and is_show=1")->count();
		$this->cate_list = BoxModel("sns_group")->get("type='group' and store_id='$this->store_id' and is_show=1");
		$where = "store_id='$this->store_id'";
		$this->group_id = $group_id = I('group_id', "0", "intval");
		if ($group_id) {
			$where .= " and pid=$group_id";
		}
		if ($user_id = I('user_id')) {
			$where .= " and user_id=$user_id";
		}
		$this->groupdata = BoxModel("sns_group")->where(array('id' => $group_id))->find();
		if ($this->groupdata) {
			$this->group_count = BoxModel("sns_topic")->where("store_id='$this->store_id' and is_show=1 and pid='$group_id'")->count();
		}
		$this->pageName = $this->pageName . "_" . $group_id;
		$this->where = $where;
		$this->display();
	}

	/******详情**/
	public function detail() {
		$this->topic_id = I('topic_id');
		$this->data = BoxModel("sns_topic")->load($this->topic_id);
		$this->display();
	}

	/********回复***/
	public function add_comment() {
		$this->pid = I('topic_id');
		$this->comment_id = I('comment_id');
		$this->data = BoxModel("SnsTopic")->load($this->pid);
		$this->tip_title = strip_tags($this->data['title']);
		$this->type = "group";
		if ($this->comment_id) {
			$data = BoxModel("SnsComment")->load("group", $this->comment_id);
			if ($data) {
				$this->pid = $data['pid'];
				$this->data = $data['pidData'];
				$this->to_user_id = $data['user_id'];
				$this->tip_title = strip_tags($data['content']);
			}
		}
		$this->display();
	}

	public function addTopic() {
		$data = I();
		$content = $data['content'];
		if (empty($data['imgList']) && is_array($data['imgList'])) {
			exit(json_encode(array('error' => 3, 'msg' => '至少上传一张图片')));
		}
		$imgListContent = "";
		if ($data['imgList']) {
			foreach ($data['imgList'] as $one) {
				$imgListContent .= '<img src="' . $one['file_url'] . '" >';
			}
		}
		$data['content'] = $imgListContent . $content;
		$r = Logic('BoxSns', 'Topic')->add($data);
		exit(json_encode($r));

	}

	/********回答***/
	public function do_comment() {
		$row = array(
			'content' => urlencode(I('content')), //必填
			'pid' => I('pid'),
			'type' => I('type'), //
			'to_comment_id' => I('to_comment_id', "0", "intval"),
		);
		$r = Logic('BoxSns', 'Comment')->add($row);
		exit(json_encode($r));
	}

	//删除评论
	public function del_comment() {
		$comment_id = I('comment_id', "0", "intval");
		$r = Logic('BoxSns', 'Comment')->del($comment_id);
		exit(json_encode($r));
	}

	//删除帖子
	public function del_topic() {
		$id = I('id', "0", "intval");
		$r = Logic('BoxSns', 'Topic')->del(array('topic_id' => $id));
		exit(json_encode($r));
	}
	//点赞
	public function add_goods() {
		$id = I('id', "0", "intval");
		if (!$this->user_id) {
			exit(json_encode(array('error' => 1, 'msg' => '请刷新页面登录后再试试')));
		}
		$topic = BoxModel("sns_topic")->where(array('id' => $id))->find();
		$row = array('topic_id' => $id, 'user_id' => $this->user_id);
		$data = BoxModel("sns_topic_goods")->where($row)->find();
		if ($data) {
			$r = BoxModel("sns_topic_goods")->where($row)->delete();
			if ($r) {
				BoxModel("sns_topic")->where(array('id' => $id))->setDec("good_count");
			}
			$act = "del";
		} else {
			$row['to_user_id'] = $topic['user_id'];
			$row['addtime'] = time();
			$row['ip']=getIp();
			$r = BoxModel("sns_topic_goods")->add($row);
			if ($r) {
				BoxModel("sns_topic")->where(array('id' => $id))->setInc("good_count");
			}
			$act = "add";
			//点赞数加1
			BoxModel("SnsNotificationCount")->inc($row['to_user_id'], "goods");
		}
		$res = array(
			'error' => $r ? 0 : 1,
			'msg' => $act,
		);
		exit(json_encode($res));
	}
	//加入小组
	public function joinGroup() {
		$group_id = I('id', "0", "intval");
		if ($this->user_id) {
			$this->group_user = BoxModel("sns_group_user")->where(array('group_id' => $group_id, 'user_id' => $this->user_id))->find();
		}
		if ($this->group_user) {
			$r = Logic('BoxSns', 'Group')->quit_group($group_id);
			$r['type'] = "quit";
		} else {
			$r = Logic('BoxSns', 'Group')->join_group($group_id);
			$r['type'] = "join";
		}

		echo json_encode($r);
	}
//添加小组
	public function add() {
		$this->group_id = $group_id = I('group_id', 0, "intval");
		$this->type = "group";
		$this->data = BoxModel("sns_group")->load($group_id);
		$this->display();
	}

}
